var chefClass = require('./../Class/ChefClass')
var resLoader = require('./../function/resLoader')
var restaurants = require('restaurants')
var UserData = require('./../UserData')
var COMMON_VALUE = require('Common')
cc.Class({
    extends: cc.Component,

    properties: {
        chefLetterPrefab:{
            default:null,
            type:cc.Prefab
        },
        showScenePrefab:{
            default:null,
            type:cc.Prefab
        },

    },
    setShowScene(self,origin){//通过
        if(self.parent.getComponent(cc.Canvas)){            
            origin.showScene = cc.instantiate(origin.showScenePrefab)
            self.parent.insertChild(origin.showScene)
            origin.showScene.active = true

            origin.showScene.getChildByName('label').getComponent(cc.Label).string = origin.node.getComponent('ResSetup').showName
            return 
        }else{
            origin.setShowScene(self.parent,origin)
        }
    },
    recruit(){
        if(this.price<=UserData.getCurrentMoney()){
            if(this.UserData.runtimeState.chefTypeList.length!=0){
                this.chefDataList = this.UserData.runtimeState.chefTypeList    
            }else{
                this.UserData.chefTypes.getAllChefTypes().then((chefTypes)=>{
                    console.log("chefTypes",chefTypes)
                    this.UserData.runtimeState.chefTypeList = chefTypes.data
                })
                return
            }
            this.chefChooseList = []
            if(!this.chefEnabled){
                this.setShowScene(this.node,this)
                this.recruitI = 0
                this.runIntoActionArr = []
                for(var i=0;i<this.letterNum;i++){
                    this.runIntoActionArr.push(cc.callFunc(this.recuritAnime,this,i+1),cc.delayTime(0.2))                
                }
                let recuritAnime = cc.sequence(this.runIntoActionArr)
                this.node.runAction(recuritAnime)
            }
            
        }
    },
    
    recuritAnime(self,letterOrder){//self需要传入this指针        
        self = self.getComponent('Recruit')
        if(self){
            self.recruitI+=1
            let a = cc.instantiate(self.chefLetterPrefab)
            if(letterOrder ===this.letterNum){
                a.getComponent('chefLetter').lastLetter =true
            }else{
                a.getComponent('chefLetter').lastLetter =false
            }
            if(self.chefDataList){
                a.getComponent("chefLetter").loadChefData(self.getRandomChef(self.chefDataList))
                a.getComponent('chefLetter').res = self.node
                self.showScene.insertChild(a,0)
                self.chefChooseList.push(a)
            }
        }        
    },
    checkRecruitButton(self){
        //self.recruitButton.getComponent(cc.Button).interactable
        self.price = COMMON_VALUE.CHEF_RECRUIT_COST_BETWEEN_FLOOR_LIST[self.node.getComponent('Restaurant').floor-1]
        if(!self.price){
            self.price = ""
        }
        self.recruitButton.getChildByName('Cost Label').getComponent("labelFixed").setNumber(self.price)

        if(self.price<=UserData.getCurrentMoney()){
            self.recruitButton.getComponent(cc.Button).interactable = true
        }else{
            self.recruitButton.getComponent(cc.Button).interactable = false
        }
    },
    recruitConfirm(node){        
        for(var i=0;i<this.chefChooseList.length;i++){
            if(node === this.chefChooseList[i]){                
                //loadChefData
                this.chefData = node.getComponent('chefLetter').chefData
                for(var j=0;j<this.UserData.state.restaurantList.length;j++){
                    if(this.UserData.state.restaurantList[j]){
                        if(this.UserData.state.restaurantList[j].floor ===this.node.getComponent('Restaurant').floor){
                            this.chefData.floor = this.UserData.state.restaurantList[j].floor
                            this.UserData.state.restaurantList[j].chef = this.chefData
                        }
                    }                    
                }
                this.node.getComponent('Restaurant').chefData = this.chefData
                this.node.getComponent('Restaurant').chefEnabled  = true
                this.node.getChildByName('Status Bar').getChildByName('Chef').active =false
                this.node.getChildByName('Status Bar').getChildByName('头像框').active =true
                let avatarSprite = this.node.getChildByName('Status Bar').getChildByName('头像框').getChildByName('Chef Photo').getComponent(cc.Sprite)
                
                let offset = COMMON_VALUE.CHEF_AVATAR_OFFSET[parseInt(this.chefData.basicData.chefID)-1]
                let chefphoto = this.node.getChildByName('Status Bar').getChildByName('头像框').getChildByName('Chef Photo')
                chefphoto.x =  offset[0]
                chefphoto.y =  offset[1]
                resLoader.plistLoader("Chef/Avatar/头像plist" , this.chefData.basicData.chefID,avatarSprite)
                //resLoader.localImgLoader('Restaurant/UI/主厨头像底色',this.node.getChildByName('Status Bar').getChildByName('Chef').getComponent(cc.Sprite))
                this.showScene.active =false

                //更新本地数据
                this.UserData.state.restaurantList[this.chefData.floor-1].chefData = this.chefData
                //更新远程数据
                let chefID = this.chefData.basicData.chefID
                let cs = this.chefData.chefRadar
                restaurants.setRestaurant(0,this.node.getComponent('Restaurant').floor,{chefID:chefID,chefSkills:cs,chefLevel:1}).then().catch((err)=>{
                    cc.log(err)
                })
                
                this.node.getChildByName('Status Bar').getChildByName('Chef').getComponent(cc.Button).interactable = false
            }
            //销毁所有求职信
            this.chefChooseList[i].destroy()
            this.showScene.destroy()
            if(this.node.getComponent('Restaurant').waitAccountSprite){//招聘后自动结束当前的等待结账按钮
                if(this.node.getComponent('Restaurant').waitAccountSprite.active){
                    this.node.getComponent('Restaurant').startWork()
                }
            }
        }
        
        UserData.subCurrentMoney(this.price)
    },

    getRandomChef(chefDataList){
        let a = chefDataList[Math.floor(Math.random()*(chefDataList.length))]
        return a
    },
    // LIFE-CYCLE CALLBACKS:    
    // onLoad () {},
    start () {
        this.recruitButton = this.node.getChildByName('Status Bar').getChildByName('Chef')
        this.UserData = UserData
        this.letterNum = 3
        UserData.runtimeState.buttonUpdateList.push([this.checkRecruitButton,this])
        
    },
    // update (dt) {},
});
